#!/usr/bin/env python
#coding: utf-8

##############################################

import types
import re,os
import logging as log

def u2g(string,code='utf8'):
	if type(string) != types.StringType:
		string=str(string)
	gbk ='gb2312'	
	return unicode(string,code).encode(gbk) 

def g2u(string,code='gb2312'):
	if type(string) != types.StringType:
		string=str(string)
	en ='utf8'	
	return unicode(string,code).encode(en) 

def removeSpace(string):
	return re.sub(r'&nbsp;','',string)



#====================================================================
class History:
	def __init__(self,historyFile):
		log.info('new history '+historyFile)
		self.historyFile = historyFile
		self.history={}
		if self.historyFile != None:
			self.loadHistoryFile()
			
	
	def exist(self,url):
		return self.history.has_key(url)	
	
	def addUrl(self,url,save = 1):
		log.debug('add a new url '+url)
		self.history[url] = url+'\n'	
		if save == 1:
			self.saveHistory()

	def saveHistory(self):
		f = open(self.historyFile,'a')
		f.writelines(self.history.values())
		f.close()

	def loadHistoryFile(self,historyFile = None):
		if historyFile != None:			
			self.historyFile = historyFile	
		else:
			if os.path.exists(self.historyFile):
				try:
					f = open(self.historyFile,'r')
				except str:
					print str
				else:
					self.history = None
					keys = f.readlines()
					self.history = {}
					for url in keys:
						if url != None:
							self.history[url] = url
			
#====================================================================

if __name__=="__main__":
	h = History('url_history.log')
#	h.loadHistoryFile()
	h.addUrl('http://www.koubei.com')
	print h.exist('http://www.koubei.com')
	print h.history
	
